package GUI;

import Run_System.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class DisplayExercisesPanel extends JPanel {
    public static final int DEFAULT_WIDTH = 800;
    public static final int DEFAULT_HEIGHT = 600;
    private Equation[] exercises_arr;
    private int currentEquation = 0;

    private ArrayList<Integer> answers;          //存放小明输入的答案


    public DisplayExercisesPanel(Exercises exercises,ArrayList<Integer> answers){
        this.exercises_arr = exercises.toArray();
        this.answers = answers;
        for(int i = 0; i < exercises.getSize(); i++){
            answers.add(-1);
        }
        setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
        setLayout(new BorderLayout());

        JLabel topLabel = new JLabel("好好学习，天天向上");
        topLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(topLabel,BorderLayout.NORTH);


        ShowEquationPanel showPanel = new ShowEquationPanel(exercises_arr,answers,currentEquation);  //生成习题面板，左侧算式，右侧结果输入区
        add(showPanel,BorderLayout.CENTER);
        currentEquation = showPanel.returnCurrentEquation();


        JPanel toolPanel = new JPanel();
        JButton saveBtn = new JButton("保存");
        saveBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    ArrayList<Boolean> results = FileOperator.exercisesCorrecting(exercises.iterator(), answers);
                    System.out.println("输出答案");
                    for(int i: answers){
                        System.out.println(i);
                    }
                    FileOperator.writeExercisesFile(exercises.iterator());  //写入习题
                    FileOperator.writeAnswer(answers);                     //写入答案
                    FileOperator.writeResults(results);                    //写入批改结果
                }catch (Exception exception){
                    exception.printStackTrace();
                    System.out.println("输入答案不是正确的数字类型");
                }
            }
        });
        toolPanel.add(saveBtn);


            JButton nextBtn = new JButton("下一页习题");
            nextBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    ShowEquationPanel showPanel_next = new ShowEquationPanel(exercises_arr,answers,currentEquation); //更新显示下一页习题
                    remove(showPanel);
                    add(showPanel_next,BorderLayout.CENTER);
                    currentEquation = showPanel_next.returnCurrentEquation();
                    if(currentEquation == exercises_arr.length){     //检查习题是否输出完毕，完毕则删除下一页习题按钮，生成返回主页按钮
                        toolPanel.remove(nextBtn);
                        JButton returnBtn = new JButton("返回主页");
                        returnBtn.addActionListener(new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                removeAll();
                                add(new FrontPage());
                                validate();
                            }
                        });
                        toolPanel.add(returnBtn);
                    }
                    validate();
                }
            });
            toolPanel.add(nextBtn);
            add(toolPanel,BorderLayout.SOUTH);
        }


    public static void main(String[] args) {
        Exercises exercises = new Exercises();
        exercises.generateAddExercises(15);
       JPanel panel = new DisplayExercisesPanel(exercises,new ArrayList<Integer>());
        JFrame frame = new JFrame();
        frame.setSize(852,543);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.add(panel);
        frame.setVisible(true);
    }
}


